﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Data.SqlClient;
using System.Data;
using GovernmentOfficeProject.SysTools;
using System.Text;
namespace GovernmentOfficeProject.LevelDataAccess
{
    public class DAL_USER_Role
    {

        /// <summary>
        /// 根据角色ID,获取角色对象.
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <returns>角色对象</returns>
        public Entity_USER_Role getRoleByRoleID(decimal roleID)
        {
            try
            {
                string strSQL = "select * from [PER_ROLE] where RoleID = @roleID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@roleID",roleID)
                };
                DataSet dst = DAL_DBGM.GetDataSet(strSQL, values);
                Entity_USER_Role role = new Entity_USER_Role();
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    role.RoleID = Convert.ToDecimal(row["RoleID"]);
                    role.RoleName = Convert.ToString(row["RoleName"]);
                    role.RoleDesc = Convert.ToString(row["RoleDesc"]);
                    role.CreateUserID = Convert.ToDecimal(row["RoleCreateUserID"]);
                    role.CreateDateTime = Convert.ToString(row["RoleCreateDate"]);
                    role.ModifyUserID = Convert.ToDecimal(row["RoleModifyUserID"]);
                    role.ModifyDateTime = Convert.ToString(row["RoleModifyDate"]);
                    role.RecordStatus = Convert.ToDecimal(row["RoleRecordStatus"]);
                }
                return role;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Role.getRoleByRoleID(decimal roleID)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据用户ID,获取该用户对应的角色列表.
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>用户对应角色列表</returns>
        public List<Entity_USER_Role> getRoleListByUserID(decimal userID)
        {
            try
            {
                string strSQL = "select B.* from [PER_USER] A,[PER_ROLE] B,[PER_USER_ROLE] M WHERE A.UserID = M.URUserID AND B.RoleID = M.URRoleID	AND M.URUserID = @userID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@userID",userID)
                };
                DataSet dst = DAL_DBGM.GetDataSet(strSQL, values);
                List<Entity_USER_Role> roleList = new List<Entity_USER_Role>();
                Entity_USER_Role role = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    role = new Entity_USER_Role();
                    role.RoleID = Convert.ToDecimal(row["RoleID"]);
                    role.RoleName = Convert.ToString(row["RoleName"]);
                    role.RoleDesc = Convert.ToString(row["RoleDesc"]);
                    role.CreateUserID = Convert.ToDecimal(row["RoleCreateUserID"]);
                    role.CreateDateTime = Convert.ToString(row["RoleCreateDate"]);
                    role.ModifyUserID = Convert.ToDecimal(row["RoleModifyUserID"]);
                    role.ModifyDateTime = Convert.ToString(row["RoleModifyDate"]);
                    role.RecordStatus = Convert.ToDecimal(row["RoleRecordStatus"]);
                    roleList.Add(role);
                }
                return roleList;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Role.getRoleListByUserID(decimal userID)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 获得所有角色.
        /// </summary>
        /// <returns>所有角色</returns>
        public List<Entity_USER_Role> getRoleList()
        {
            try
            {
                string strSQL = "select * from [PER_ROLE]";
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                List<Entity_USER_Role> roleList = new List<Entity_USER_Role>();
                Entity_USER_Role role = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    role = new Entity_USER_Role();
                    role.RoleID = Convert.ToDecimal(row["RoleID"]);
                    role.RoleName = Convert.ToString(row["RoleName"]);
                    role.RoleDesc = Convert.ToString(row["RoleDesc"]);
                    role.CreateUserID = Convert.ToDecimal(row["RoleCreateUserID"]);
                    role.CreateDateTime = Convert.ToString(row["RoleCreateDate"]);
                    role.ModifyUserID = Convert.ToDecimal(row["RoleModifyUserID"]);
                    role.ModifyDateTime = Convert.ToString(row["RoleModifyDate"]);
                    role.RecordStatus = Convert.ToDecimal(row["RoleRecordStatus"]);
                    roleList.Add(role);
                }
                return roleList;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Department.getDeptartmentList()---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据传入角色实体的参数,获得多个角色列表.
        /// </summary>
        /// <param name="roleEntity"></param>
        /// <returns></returns>
        public List<Entity_USER_Role> getRoleListByRoleEntity(Entity_USER_Role roleEntity)
        {
            try
            {
                string strSQL = "select * from [PER_ROLE] where 1=1";
                if (roleEntity.RoleName != "" && roleEntity.RoleName != null)
                {
                    strSQL += " and RoleName='"+roleEntity.RoleName+"'";
                }
                if (roleEntity.RoleDesc != "" && roleEntity.RoleDesc != null)
                {
                    strSQL += " and RoleDesc='" + roleEntity.RoleDesc + "'";
                }
                if (roleEntity.CreateUserID != -1)
                {
                    strSQL += " and RoleCreateUserID = " + roleEntity.CreateUserID;
                }
                if (roleEntity.CreateDateTime != "" && roleEntity.CreateDateTime != null)
                {
                    strSQL += " and RoleCreateDate = '" + roleEntity.CreateDateTime + "'";
                }
                if (roleEntity.ModifyUserID != -1)
                {
                    strSQL += " and RoleModifyUserID = " + roleEntity.ModifyUserID;
                }
                if (roleEntity.ModifyDateTime != "" && roleEntity.ModifyDateTime != null)
                {
                    strSQL += " and RoleModifyDate = '" + roleEntity.ModifyDateTime + "'";
                }
                if (roleEntity.RecordStatus != -1)
                {
                    strSQL += " and RoleRecordStatus = " + roleEntity.RecordStatus;
                }
                //AppLog.Write("getRoleListByRoleEntity-SQL:" + strSQL, AppLog.LogMessageType.Debug);
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                List<Entity_USER_Role> roleList = new List<Entity_USER_Role>();
                Entity_USER_Role role = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    role = new Entity_USER_Role();
                    role.RoleID = Convert.ToDecimal(row["RoleID"]);
                    role.RoleName = Convert.ToString(row["RoleName"]);
                    role.RoleDesc = Convert.ToString(row["RoleDesc"]);
                    role.CreateUserID = Convert.ToDecimal(row["RoleCreateUserID"]);
                    role.CreateDateTime = Convert.ToString(row["RoleCreateDate"]);
                    role.ModifyUserID = Convert.ToDecimal(row["RoleModifyUserID"]);
                    role.ModifyDateTime = Convert.ToString(row["RoleModifyDate"]);
                    role.RecordStatus = Convert.ToDecimal(row["RoleRecordStatus"]);
                    roleList.Add(role);
                }
                return roleList;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Role.getRoleListByRoleEntity(Entity_USER_Role roleEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 增加一个角色.
        /// </summary>
        /// <param name="roleEntity">传入角色实体</param>
        /// <returns>true:成功;false:失败.</returns>
        public bool addRole(Entity_USER_Role roleEntity)
        {
            try
            {

                //sbrSQL.Append("insert into [PER_ROLE](RoleName,RoleDesc,RoleCreateUserID,RoleCreateDate,RoleModifyUserID,RoleModifyDate,RoleRecordStatus)");
                //sbrSQL.Append(" values(@roleName,@roleDesc,@roleCreateUserID,@roleCreateDate,@roleModifyUserID,@roleModifyDate,@roleRecordStatus)");
                bool flag = false;
                bool blState = false;
                StringBuilder sqlBudIns = new StringBuilder();
                StringBuilder sqlBudValue = new StringBuilder();
                sqlBudIns.Append("insert into [PER_ROLE](");
                sqlBudValue.Append(" values(");
                List<SqlParameter> sprList = new List<SqlParameter>();
                if (roleEntity.RoleName!="" && roleEntity.RoleName!=null)
                {
                    sqlBudIns.Append("RoleName,");
                    sqlBudValue.Append("@roleName,");
                    sprList.Add(new SqlParameter("@roleName", roleEntity.RoleName));
                    blState = true;
                }
                if (roleEntity.RoleDesc!="" && roleEntity.RoleDesc!=null)
                {
                    sqlBudIns.Append("RoleDesc,");
                    sqlBudValue.Append("@roleDesc,");
                    sprList.Add(new SqlParameter("@roleDesc", roleEntity.RoleDesc));
                    blState = true;
                }
                if (roleEntity.CreateUserID != -1)
                {
                    sqlBudIns.Append("RoleCreateUserID,");
                    sqlBudValue.Append("@roleCreateUserID,");
                    sprList.Add(new SqlParameter("@roleCreateUserID", roleEntity.CreateUserID));
                    blState = true;
                }
                if (roleEntity.CreateDateTime!="" && roleEntity.CreateDateTime!=null)
                {
                    sqlBudIns.Append("RoleCreateDate,");
                    sqlBudValue.Append("@roleCreateDate,");
                    sprList.Add(new SqlParameter("@roleCreateDate", roleEntity.CreateDateTime));
                    blState = true;
                }                 
                if (roleEntity.ModifyUserID != -1)
                {
                    sqlBudIns.Append("RoleModifyUserID,");
                    sqlBudValue.Append("@roleModifyUserID,");
                    sprList.Add(new SqlParameter("@roleModifyUserID", roleEntity.ModifyUserID));
                    blState = true;
                }
                if (roleEntity.ModifyDateTime!="" && roleEntity.ModifyDateTime!=null)
                {
                    sqlBudIns.Append("RoleModifyDate,");
                    sqlBudValue.Append("@roleModifyDate,");
                    sprList.Add(new SqlParameter("@roleModifyDate", roleEntity.ModifyDateTime));
                    blState = true;
                }
                if (roleEntity.RecordStatus != -1)
                {
                    sqlBudIns.Append("RoleRecordStatus,");
                    sqlBudValue.Append("@roleRecordStatus,");
                    sprList.Add(new SqlParameter("@roleRecordStatus", roleEntity.RecordStatus));
                    blState = true;
                }
                 
                if (blState)
                {
                    string strSQL = sqlBudIns.ToString().Substring(0, sqlBudIns.Length - 1) + ")" + sqlBudValue.ToString().Substring(0, sqlBudValue.Length - 1) + ")";
                    SqlParameter[] values = sprList.ToArray();
                    AppLog.Write("DAL-增加用户的SQL语句: " + strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.InsertInfo(strSQL, values);
                }
                else
                {
                    //添加用户失败:没有任何数据.
                    AppLog.Write("添加用户失败:没有任何数据.", AppLog.LogMessageType.Info);
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Role.addRole(Entity_USER_Role roleEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据角色ID,修改角色资料.
        /// </summary>
        /// <param name="roleID">待修改角色ID</param>
        /// <param name="roleEntity">待修改的角色参数实体类</param>
        /// <returns>true:成功;false:失败.</returns>
        public bool updateRoleByRoleID(decimal roleID, Entity_USER_Role roleEntity)
        {
            try
            {
                bool flag = false;
                bool blstate = false;
                string strSQL = "update [PER_ROLE] set";
                if (roleEntity.RoleName != "" && roleEntity.RoleName != null)
                {
                    strSQL += " RoleName='" + roleEntity.RoleName + "',";
                    blstate = true;
                }
                if (roleEntity.RoleDesc != "" && roleEntity.RoleDesc != null)
                {
                    strSQL += " RoleDesc='" + roleEntity.RoleDesc + "',";
                    blstate = true;
                }
                if (roleEntity.CreateUserID != -1)
                {
                    strSQL += " RoleCreateUserID = " + roleEntity.CreateUserID + ",";
                    blstate = true;
                }
                if (roleEntity.CreateDateTime != "" && roleEntity.CreateDateTime != null)
                {
                    strSQL += " RoleCreateDate = '" + roleEntity.CreateDateTime + "',";
                    blstate = true;
                }
                if (roleEntity.ModifyUserID != -1)
                {
                    strSQL += " RoleModifyUserID = " + roleEntity.ModifyUserID+",";
                    blstate = true;
                }
                if (roleEntity.ModifyDateTime != "" && roleEntity.ModifyDateTime != null)
                {
                    strSQL += " RoleModifyDate = '" + roleEntity.ModifyDateTime + "',";
                    blstate = true;
                }
                if (roleEntity.RecordStatus != -1)
                {
                    strSQL += " RoleRecordStatus = " + roleEntity.RecordStatus+",";
                    blstate = true;
                }
                if (blstate)
                {
                    strSQL = strSQL.Substring(0,strSQL.Length - 1) + " where RoleID=@roleID";
                    SqlParameter[] values = new SqlParameter[]{
                        new SqlParameter("@roleID",roleID)
                    };
                    flag = DAL_DBGM.UpdateInfo(strSQL, values);
                    return flag;
                }
                else
                {
                    //没有需要更新的内容.
                    return flag;
                }                
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Role.updateRoleByRoleID(decimal roleID, Entity_USER_Role roleEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据角色ID,删除对应角色.
        /// </summary>
        /// <param name="roleID">待删除角色ID</param>
        /// <returns>true:成功;false:失败.</returns>
        public bool deleteRoleByRoleID(decimal roleID)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete from [PER_ROLE] where RoleID=@roleID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@roleID",roleID)
                };
                flag = DAL_DBGM.DeleteInfo(strSQL, values);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_Role.deleteRoleByRoleID(decimal roleID)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }
    }
}
